//
//  PhoneLandViewController.m
//  悦享
//
//  Created by 韩双 on 16/3/28.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "PhoneLandViewController.h"

@interface PhoneLandViewController ()

@property (nonatomic, retain) UITextField *telepnone;   /**< 手机号 */
@property (nonatomic, retain) UITextField *passwordField;  /**< 密码 */

@property (nonatomic, retain) NSMutableArray *phoneArr;  /**< 查询到的账号(phone)数组 */
@property (nonatomic, copy) NSString *password;    /**< 匹配上的密码 */

@property (nonatomic, retain) UIAlertController *alertVC;

@end

@implementation PhoneLandViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [self updateBmob];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createSubviews];
    [self createAlertVC];
}

#pragma mark - 初始化页面
- (void)createSubviews {
    
    self.navigationItem.title = @"手机号登录";
    
    UIBarButtonItem *resignItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(resignAction:)];
    self.navigationItem.rightBarButtonItem = resignItem;
    
    UILabel *phone = [[UILabel alloc] init];
    [self.view addSubview:phone];
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view).with.offset(100);
        make.left.equalTo(self.view).with.offset(20);
        make.size.mas_equalTo(CGSizeMake(60, 20));
        
    }];
    phone.text = @"手机号";
    
    self.telepnone = [[UITextField alloc] init];
    [self.view addSubview:_telepnone];
    [_telepnone mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(phone).with.offset(80);
        make.centerY.mas_equalTo(phone);
        make.right.equalTo(self.view).with.offset(-20);
        
    }];
    _telepnone.clearButtonMode = UITextFieldViewModeWhileEditing;
    _telepnone.placeholder = @"请输入手机号";
    _telepnone.keyboardType = UIKeyboardTypeNumberPad;  // *****弹出键盘的类型（枚举值）数字键盘
    
    UILabel *password = [[UILabel alloc] init];
    [self.view addSubview:password];
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(phone);
        make.size.mas_equalTo(phone);
        make.top.equalTo(phone).with.offset(80);
        
    }];
    password.text = @"密码";
    
    self.passwordField = [[UITextField alloc] init];
    [self.view addSubview:_passwordField];
    [_passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(_telepnone);
        make.centerY.mas_equalTo(password);
        make.size.equalTo(_telepnone);
        
    }];
    _passwordField.placeholder = @"请输入密码";
    _passwordField.secureTextEntry = YES;
    
    UIButton *landButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:landButton];
    
    [landButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
        make.top.equalTo(_passwordField).with.offset(50);
        make.height.mas_equalTo(40);
        
    }];
    [landButton setTitle:@"登录" forState:UIControlStateNormal];
    [landButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    landButton.backgroundColor = [UIColor colorWithRed:0.9859 green:0.3521 blue:0.4104 alpha:1.0];
    landButton.layer.cornerRadius = 8;
    [landButton addTarget:self action:@selector(landAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}

#pragma mark - 查询bmob
- (void)updateBmob {
    
    self.phoneArr = [NSMutableArray array];
    
    BmobQuery *user_info = [BmobQuery queryWithClassName:@"user_info"];
    //查找user_info表的数据
    [user_info findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            
            NSString *phone = [obj objectForKey:@"phone"];
            
            [self.phoneArr addObject:phone];
        }
        NSLog(@"查询结果%@", self.phoneArr);
    }];
}

#pragma mark - 注册
- (void)resignAction:(UIBarButtonItem *)resign {
    
    NSLog(@"注册");
    
    ResignViewController *resignVC = [[ResignViewController alloc] init];
    
    resignVC.number = _telepnone.text;
    
    [self.navigationController pushViewController:resignVC animated:YES];
    
}

#pragma mark - 创建alertVC
- (void)createAlertVC {
    
    self.alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    
    [_alertVC addAction:action];
    
}

#pragma mark - 登录
- (void)landAction:(UIButton *)land {

    if (_telepnone.text.length == 0 || _passwordField.text.length == 0) {
        
        _alertVC.title = @"请输入正确账号或密码";
        [self presentViewController:_alertVC animated:YES completion:nil];
        
    }else if (![_phoneArr containsObject:_telepnone.text]) {

        _alertVC.title = @"手机号尚未注册";
        [self presentViewController:_alertVC animated:YES completion:nil];
        
        
    }else{
        BmobQuery *user_info = [BmobQuery queryWithClassName:@"user_info"];
        
        [user_info whereKey:@"phone" equalTo:_telepnone.text];
        
        [user_info findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            for (BmobObject *obj in array) {
                
                self.password = [obj objectForKey:@"password"];
                NSLog(@"%@", _password);
            }
            
            if (_password != _passwordField.text) {
                _alertVC.title = @"请输入正确密码";
                [self presentViewController:_alertVC animated:YES completion:nil];
                
            }else{
                
                _alertVC.title = @"登陆成功";
                
                [self.navigationController popToRootViewControllerAnimated:YES];
//                [self presentViewController:_alertVC animated:YES completion:^{
//                    
//                }];
                
            }
            
        }];
        
        
    }
    
    
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
